package com.accompany.fragment.my.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.view.animation.AnimationUtils;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.accompany.R;
import com.accompany.api.bean.BaseStringMsgBean;
import com.accompany.api.bean.ProvinceCityEntity;
import com.accompany.api.category.ZhaoMuPeiZhenCategory;
import com.accompany.api.presenter.ZhaoMuPeiZhenPresenter;
import com.accompany.base.BaseActivity;
import com.accompany.base.BaseBean;
import com.accompany.dialog.BaomingSuccessDialog;
import com.accompany.main.ConstantStaticUrl;
import com.accompany.main.MainActivity;
import com.accompany.utils.CountDownTimerUtils;
import com.accompany.utils.FullScreenUtils;
import com.accompany.utils.ToastUtil;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.BindView;
import butterknife.OnClick;

public class ZhaomuPeiZhenYuanActivity extends BaseActivity implements ZhaoMuPeiZhenCategory.View {
    @BindView(R.id.base_title)
    TextView mTitleMsg;
    @BindView(R.id.edit_name)
    EditText mEditName;
    @BindView(R.id.edit_mobile)
    EditText mEditMobile;
    @BindView(R.id.edit_code)
    EditText mEditCode;
    @BindView(R.id.send_code_zhao)
    TextView mSendCodeZhao;
    @BindView(R.id.city_name_pei)
    TextView mAddress_Nmae;
    @BindView(R.id.check_box)
    CheckBox mCheckBox;
    @BindView(R.id.linear_areed)
    LinearLayout mLlAgreement;
    @BindView(R.id.to_peizheng)
    TextView mSubmitPeizhegn;
    private String mName,mMobile,mSendCode;
    private ZhaoMuPeiZhenPresenter mPresenter;
    private long mCityCode;
    private String mCityName,mProvinceName;
    private long mProvinceCode;
    @Override
    public int getLayoutId() {
        return R.layout.activity_zhaomu_view;
    }
    @Override
    public void initLayoutView() {
        FullScreenUtils.getInstance().fullScreen(ZhaomuPeiZhenYuanActivity.this,true);

        mTitleMsg.setText("招募陪诊员");
        mPresenter = new ZhaoMuPeiZhenPresenter(this);
        SetPersenter(mPresenter);
        mPresenter.getProvinceCityList();
    }

    @OnClick({R.id.base_back, R.id.send_code_zhao, R.id.to_peizheng,R.id.yonghu})
    public void OnClick(View view) {
        switch (view.getId()) {
            case R.id.base_back:
                finishWitchAnimation();
                break;
            case R.id.send_code_zhao:
                mMobile = mEditMobile.getText().toString().trim();
                if (mMobile.equals("")) {
                    ToastUtil.showToastINFO(ZhaomuPeiZhenYuanActivity.this, "请输入手机号码");
                } else {
                    CountDownTimerUtils mCountDownTimerUtils = new CountDownTimerUtils(mSendCodeZhao, 60000, 1000);
                    mCountDownTimerUtils.start();
                    mPresenter.getUserCode(mMobile);
                }
                break;
            case R.id.yonghu:
                openActivityWitchAnimation(AuthorizationActivity.class);
                break;
            case R.id.to_peizheng:
                getStringMsg();
                if (mName.equals("")) {
                    ToastUtil.showToastINFO(ZhaomuPeiZhenYuanActivity.this, "请输入姓名");
                    return;
                } else if (mMobile.equals("")) {
                    ToastUtil.showToastINFO(ZhaomuPeiZhenYuanActivity.this, "请输入手机号码");
                    return;
                } else if (mSendCode.equals("")) {
                    ToastUtil.showToastINFO(ZhaomuPeiZhenYuanActivity.this, "请输入手机验证码");
                    return;
                } else if (mCheckBox.isChecked() == false) {
                    mLlAgreement.startAnimation(AnimationUtils.loadAnimation(this, R.anim.transla_checkbox));
                    return;
                }else {
                    JSONObject result = new JSONObject();
                    try {
                        result.put("recruitName", mName);
                        result.put("recruitMobile", mMobile);
                        result.put("cityId", mCityCode);
                        result.put("provId", mProvinceCode);
                        result.put("provName", mProvinceName);
                        result.put("cityName", mCityName);
                        result.put("verifyCode", mSendCode);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    mPresenter.getPeizhenMsg(result.toString());
                }
                break;
        }
    }

    private void getStringMsg() {
        mName = mEditName.getText().toString().trim();
        mMobile = mEditMobile.getText().toString().trim();
        mSendCode = mEditCode.getText().toString().trim();
    }

    @Override
    public void getProvinceCityList(ProvinceCityEntity province) {
        if (province.getCode() == ConstantStaticUrl.SUCCESS_CODE) {
            for (ProvinceCityEntity.ProvinceCityEntityData provice : province.getData()) {
                mProvinceCode = provice.getProvinceId();
                mProvinceName = provice.getProvinceName();
                for (ProvinceCityEntity.ProvinceCityEntityData.ProvinceCityEntityDataCities city : provice.getCities()) {
                    mCityCode = city.getCityId();
                    mCityName = city.getCityName();
                    mAddress_Nmae.setText(city.getCityName());
                }
            }
        } else {
            ToastUtil.showToastINFO(ZhaomuPeiZhenYuanActivity.this, province.getMsg());
        }
    }

    @Override
    public void getUserCode(BaseStringMsgBean code) {
        if (code.getCode() == ConstantStaticUrl.SUCCESS_CODE) {
            ToastUtil.showToastSUCCESS(ZhaomuPeiZhenYuanActivity.this, "验证码获取成功");
        } else {
            ToastUtil.showToastINFO(ZhaomuPeiZhenYuanActivity.this, code.getMsg());
        }
    }
    @Override
    public void getPeizhenMsg(BaseBean bean) {
        if (bean.getCode() == ConstantStaticUrl.SUCCESS_CODE) {
            BaomingSuccessDialog dialog = new BaomingSuccessDialog(ZhaomuPeiZhenYuanActivity.this);
            dialog.show();
            dialog.findViewById(R.id.baoming).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    openActivityWitchAnimation(MainActivity.class);
                    finish();
                    dialog.dismiss();


                }
            });
        } else {
            ToastUtil.showToastINFO(ZhaomuPeiZhenYuanActivity.this, bean.getMsg());
        }
    }

    @Override
    public void SetPersenter(Object persenter) {

    }

    @Override
    public void OnShowErrorMsg(String mErrorMsg) {
        ToastUtil.showToastERROR(ZhaomuPeiZhenYuanActivity.this, mErrorMsg);

    }
}
